<cfscript>
setting show=false;

function createOutput() {
    if(!LuceeAIHas('default:exception')) return false;

    var data=getHTTPRequestData();
    if(!structKeyExists(data, "content")) return false;

    
    // create session
	if(!structKeyExists(session, "exceptionAISession") || (session.exceptionAISession.id?:"") != url.id) {
        ais=LuceeCreateAISession(
            name:'id:#url.id#', 
            conversationHistoryLimit:3,
            systemMessage:"You are a Lucee expert specializing in debugging CFML applications. You will analyze exceptions that occur during Lucee (CFML) code execution.

When analyzing the exception details:
- Consider the broader context of common CFML patterns and practices
- Look for common pitfalls like scope issues, type mismatches, or missing variables
- Check for potential race conditions in thread operations
- Identify if the issue might be environment-related (server settings, Java version, etc.)
- Consider database connection issues if queries are involved
- Look for common syntax errors specific to CFML/Lucee

For your response:
- Provide a clear, concise analysis in plain HTML
- When suggesting fixes, include practical code examples
- If relevant, suggest debugging steps or logging statements
- Mention potential performance implications if applicable
- Include references to similar common issues when helpful
- Point out any potential security implications if present

HTML formatting rules:
- Use h3 as the largest heading
- Use <code class=""lucee-ml""> for multi-line code blocks
- Use <code> for inline code references
- Avoid <code> within heading tags
- Properly escape HTML in code examples with &lt;
- Keep structure flat for integration into existing error pages

Focus on actionable solutions and preventive measures. Avoid repeating the exception details that are already shown elsewhere on the page."
        );
        var meta=LuceeAIGetMetaData(ais);
        var label="#meta.label?:''# (#meta.model?:''#)";
        session.exceptionAISession={"ais":ais,"id":url.id,"label":label};
        
    }
    else {
        ais=session.exceptionAISession.ais;
        var label=session.exceptionAISession.label?:"";
    }
    // inquiry
    var aiFirst=true;
	var answer=LuceeInquiryAISession(ais,data.content,function(msg) {
        echo(msg);
		cfflush(throwonerror=false);			
	});

    // meta data
    var meta=LuceeAIGetMetaData('default:exception');
    echo("<h4>Generated by #meta.label?:''# (#meta.model?:''#)</h4>");
    // AI (#meta.label?:''#)

}
createOutput();
</cfscript>